package com.ethink.warehouse.service.impl;

import com.ethink.warehouse.common.entity.StockInOrderDetail;
import com.ethink.warehouse.common.mapper.StockInOrderDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.warehouse.api.request.StockInOrderAddRequest;
import com.ethink.warehouse.api.response.StockInOrderAddResponse;
import com.ethink.warehouse.common.entity.StockInOrder;
import com.ethink.warehouse.common.mapper.StockInOrderMapper;
import com.ethink.warehouse.converter.StockInOrderConverter;
import com.ethink.warehouse.service.StockInOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库入库单 Service接口实现
 *
 * @author default
 * @date 2022-12-05
 */
@Slf4j
@Service
public class StockInOrderServiceImpl implements StockInOrderService {

    @Autowired
    private StockInOrderMapper stockInOrderMapper;

    @Autowired
    private StockInOrderDetailMapper stockInOrderDetailMapper;

    @Autowired
    private StockInOrderConverter stockInOrderConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StockInOrderAddResponse addStockInOrder(StockInOrderAddRequest request) {
        StockInOrderAddResponse response = new StockInOrderAddResponse();
        StockInOrder stockInOrder = new StockInOrder();
        BeanUtils.copyProperties(request, stockInOrder);
        stockInOrderMapper.insert(stockInOrder);
        response.setId(stockInOrder.getId());
        // 商品明细
        request.getItems().forEach(mateItem -> {
            StockInOrderDetail stockInOrderDetail = new StockInOrderDetail();
            BeanUtils.copyProperties(mateItem, stockInOrderDetail);
            stockInOrderDetailMapper.insert(stockInOrderDetail);
        });
        return response;
    }

}
